import 'package:fl_chart/fl_chart.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/db/merroy_db.dart';

import 'package:sleep/provider/wave_deatil_provider/wave_provider.dart';
import 'package:sleep/single/analyze/pluse_analyze.dart';
import 'package:sleep/single/analyze/sleep_analyze.dart';

class TurnOverModel {
  final int turnOver;
  final List<FlSpot> turnOverSpots;

  TurnOverModel(this.turnOver, this.turnOverSpots);
}

final turnOverProvider = Provider<TurnOverModel>((ref) {
  final pluse = ref.watch(pulseProvider).analyze as pulseAnalyze;
  final sleep = ref.watch(sleepProvider).analyze as SleepAnalyze;

  int current = DateTime.now().millisecondsSinceEpoch;
  bool hasTurnOff = pluse.turnOver || sleep.turnOver;
  final preTime = MerroyDB.share().turnOverTime;
  if (preTime == null || current - preTime > 1000) {
    //2s 最小间隔
    if (hasTurnOff) MerroyDB.share().turn += 1;
    final index = (current - (preTime ?? current)) ~/ 1000;
    MerroyDB.share()
        .turnOverStates
        .add(FlSpot(index.toDouble(), MerroyDB.share().turn.toDouble()));
    MerroyDB.share().turnOverTime = current;
  }

  return TurnOverModel(MerroyDB.share().turn, MerroyDB.share().turnOverStates);
});
